package blogtest;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogTest extends InitAndEnd{
    /*
        测试登录 跳转到    http://localhost:8080/myblog_list.html
     */
    @Test
    @Order(1)
    void loginTest() throws InterruptedException {
        //打开博客登录页
            webDriver.get("http://localhost:8080/login.html");
            webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
            //输入用户名和密码
            webDriver.findElement(By.cssSelector("#username")).sendKeys("xiaowen");
            webDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
            webDriver.findElement(By.cssSelector("#submit")).click();
            //验证
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_url =  webDriver.getCurrentUrl();
        Assertions.assertEquals("http://localhost:8080/myblog_list.html",cur_url) ;
        sleep(5000);
    }
    @Test
   @Order(2)
   void writeBlog() throws InterruptedException {
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#title")).sendKeys("软件测试");
        sleep(3000);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
        sleep(3000);
        Alert alert = webDriver.switchTo().alert();
        alert.dismiss();
        sleep(3000);
        webDriver.get("http://localhost:8080/myblog_list.html");
        Set<String> handles = webDriver.getWindowHandles();
        String cur_handle = "";
        for (String handle: handles) {
            cur_handle = handle;
        }
        webDriver.switchTo().window(cur_handle);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
       String cur_title =  webDriver.findElement(By.xpath("//*[@id=\"artlist\"]/div[1]/div[1]")).getText();
       Assertions.assertEquals("软件测试",cur_title);
    }
    @Test
    @Order(3)
    void deleteBlog() throws InterruptedException {
        webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > a:nth-child(6)")).click();
        sleep(1000);
        Alert alert = webDriver.switchTo().alert();
        alert.accept();
        sleep(1000);
        alert.accept();
        sleep(3000);
        String cur_title = webDriver.findElement(By.cssSelector("#artlist > div > div.title")).getText();
        Assertions.assertEquals("11111",cur_title);
    }
    @Test
    @Order(4)
    void logout(){
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        Alert alert = webDriver.switchTo().alert();
        alert.accept();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //校验博客列表页url
        String cur_url = webDriver.getCurrentUrl();
         Assertions.assertEquals("http://localhost:8080/blog_list.html",cur_url);

         //检测第一张文章是不是 11111
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        String cur_title = webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > div.title")).getText();
        Assertions.assertEquals("11111",cur_title);
    }
//    @Test
//    void test(){
//        webDriver.get("https//www.baidu.com");
//    }

}
